<?php
	require_once 'file.php';

	class Fmgr0v0r01_text extends Fmgr0v0r01_file implements Fmgr_Text{

		var $media_class = 'Text';
		
		function set_functions(){
			static $a;
			if(!$a) $a = array_merge(parent::set_functions(), array(
				'contents',
				'append'
			));
			return $a;	
		}
		
		function summary_vars(){
			static $a;
			if(!$a) $a = array_merge_recursive(parent::summary_vars(), array(
				"2:" => array(
					'is_script', 
					'is_parsed', 
					'contents'
				)
			));
			return $a;
		}
		
		protected $is_script;
		protected $is_parsed;
		
		function root_write(){
			return (!$this->is_script() || usr()->can_script);
		}
		
####	get functions
		
		function own(){
			return ($this->is_script()) ? 'u:500' : $this->data->own;
		}
		
		function grp(){
			return ($this->is_script()) ? 'g:500' : $this->data->grp;
		}
		
		function mode(){
			return ($this->is_script()) ? '644' : $this->data->mod;
		}
		
		function icon(){
			return ($this->is_script()) ? 'script.png' : parent::icon();	
		}
				
		protected function get_body(){
			// text files return their own body or their usurpa's
			return (!$this->usurp_mode) ? parent::get_body() : $this->get_usurpa()->get_ltxt('lstr');
		}
		
		function contents($with_template=true){
			if(!$this->allow_read) return;
			if(!PUBLIC_DOMAIN || !$with_template) return $this->get_contents();
			// Sanity!
			$this->data->contents = '';
			$this->data->caller = My()->caller;
			$c = ($this->is_parsed()) ? smuoi_parse($this->get_contents(), $this) : $this->get_contents();
			if($t = $this->template()){
				My()->caller = $this;
				$this->data->contents = $c;
				$c = $t->contents();
				My()->caller = $this->data->caller;
			}
			unset($this->data->contents);
			unset($this->data->caller);
			return $c;
		}
		
		function put(){
			if(!$this->allow_read) return;
			header("Content-type: ".$this->data->mime);
			echo $this->contents();
		}
		
		function usurpa(){
			parent::usurpa();
			// usurp uris of css and javascript files
			if($this->usurp_mode && (in_array($this->xtn(), array('js', 'css')))){
				$u = $this->get_usurpa();
				$this->data->href = $u->href();
				$this->data->uri = $u->uri();
			}
			return $this;
		}
		
		protected function get_contents(){
			$rsrc = ($this->usurp_mode)? $this->get_usurpa() : $this;
			if(!PUBLIC_DOMAIN || !$this->is_script()) return file_get_contents($rsrc->realpath());
			return $rsrc->run_script();
		}
		
		protected function run_script(){
			$fp = $this->realpath();
			$x = false;
			if(ini_get('short_open_tag') && smuoi_getenv('SMUOI_ESCAPE_XPIS')){
				$c = file_get_contents($fp);
				while(preg_match('/<\?([^\s\t\n\=(php)].+)\?>/', $c, $m)){
					$mrk = "<!--XMLDEC(".utime().")!-->";
					$x[$mrk] = $m[0];
					$c = str_replace($m[0], $mrk, $c);
				}
			}
			ob_start();
			$cwd = getcwd();
			chdir(dirname($fp));
			if($x) eval(" ?>$c<?php ");
			else include $fp;
			chdir($cwd);
			$c = ob_get_clean();
			if($x) foreach($x as $mrk=>$xd) $c = str_replace($mrk, $xd, $c);
			return $c;	
		}
		
		function is_script(){
			if(!is_null($this->is_script)) return $this->is_script;
			// no extension, not a script
			if(!$x = strtolower($this->xtn())) return $this->is_script = false;
			// php hardcoded, is script
			if($x == 'php') return $this->is_script = true;
			// not in the public docroot, must be .php
			if($this->rdk() !== 'docs') return $this->is_script = false;
			// read the environment variables in the public domain
			if(PUBLIC_DOMAIN) return $this->is_script = smuoi_xtn_is_script($x);
			// in the SMRDE, we treat files permitted handover as scripts since permissions are relevant
			$ht = $this->parent()->htrules();
			return $this->is_script = ($ht->SCRIPT->$x || $ht->PERMIT_HANDOVER->$x);
		}
		
		function is_parsed(){
			if(!is_null($this->is_parsed)) return $this->is_parsed;
			if(!$x = strtolower($this->xtn())) return $this->is_parsed = false;
			if(!PUBLIC_DOMAIN){ 
				// is_parsed is not so easy to detect in the SMRDE context since the value is inherited from the caller
				if($this->rdk() !== 'docs') return $this->is_parsed = false;
				return $this->is_parsed = $this->parent()->htrules()->PARSE->$x;
			}
			return (caller() === $this) ? smuoi_xtn_is_parsed($x) : caller()->is_parsed();
		}

####	set functions
		
		protected function set_contents($string, $mode='w'){
			$fh = fopen($this->realpath(), $mode);
			fwrite($fh, $string);
			fclose($fh);
			$this->add_sanity_check('filesize');
			return false;
		}
		
		protected function set_append($string){
			return $this->set_contents($string, 'a+');
		}
		
		protected function post_commit(){
			parent::post_commit();
			$this->is_parsed = $this->is_script = null;
		}
	
	}

?>